/*
 * $Id$
 * Copyright (c) 2000, 2010 Apusic Software Inc.
 * All rights reserved
 */
package com.supersystem.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <li>Brief:
 * <li>Description:
 * @author  luoyg
 * @version
 * @see
 * @since
 */

public class Page extends PageQueryParameter implements Serializable {
    private static final long serialVersionUID = 201111261559L;

    /**
     * 总记录条数
     */
    private int total;
    /**
     * 当前页记录
     */
    private List<?> rows = new ArrayList<Object>();
    
    /**
     * 当前页码
     */
    private int page;

    public Page(PageQueryParameter pageQueryParameter) {
        this(pageQueryParameter.getPageNo(), pageQueryParameter.getPageSize());
    }

    public Page(int pageNo, int pageSize) {
        super(pageNo, pageSize);
    }

    public static final Page EMPTY_PAGE = new Page(0, 0) ;

    /**
     * 取得总页数
     * @return
     */
    public int getPages() {
        return total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
    }

    /**
     * 上一页页码
     * @return
     */
    public int prev() {
        if (total == 0) {
            return 1;
        }
        return pageNo == 1 ? 1 : pageNo - 1;
    }
    /**
     * 下一页页码
     * @return
     */
    public int next() {
        if (total == 0) {
            return 1;
        }
        return pageNo + 1 > getPages() ? getPages() : pageNo + 1;
    }
    /**
     * 最后一页页码
     * @return
     */
    public int last() {
        if (total == 0) {
            return 1;
        }
        return getPages();
    }
    /**
     * 取得总记录条数
     * @return
     */
    public int getTotal() {
        return total;
    }

    public int getLastIndex(){
        return this.getFirstIndex() + rows.size();
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public <T> List<T> getRecords() {
        return (List<T>)rows;
    }
    public void setRecords(List<?> rows) {
        this.rows = rows;
    }

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
    
    
}
